<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="88px">
      <el-form-item label="项目类别" prop="projectTyoe">
        <el-select v-model="queryParams.projectTyoe" placeholder="请选择项目类别" clearable>
          <el-option
              v-for="dict in project_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="项目负责人" prop="projectPersonChargeName">
        <el-input
            v-model="queryParams.projectPersonChargeName"
            placeholder="请输入项目负责人名称"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="申报状态" prop="projectApplicationState">
        <el-select v-model="queryParams.projectApplicationState" placeholder="请选择项目流程申报状态" clearable>
          <el-option
              v-for="dict in project_app_process_state"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="开始时间" prop="projectStartTime">
        <el-input
            v-model="queryParams.projectStartTime"
            placeholder="请输入项目开始时间"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="结束时间" prop="projectEndTime">
        <el-input
            v-model="queryParams.projectEndTime"
            placeholder="请输入项目结束时间"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            type="primary"
            plain
            @click="handleAdd"
            v-hasPermi="['departmentWaterResources:project:add']"
        >申报项目</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="danger"
            plain
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['departmentWaterResources:project:remove']"
        >删除项目</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="danger"
            plain
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['departmentWaterResources:project:remove']"
        >取消上报</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="warning"
            plain
            @click="handleExport"
            v-hasPermi="['departmentWaterResources:project:export']"
        >导出PDF</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="projectList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center">
        <template #default="scope">
          {{ scope.$index + (queryParams.pageNum - 1) * queryParams.pageSize + 1 }}
        </template>
      </el-table-column>
      <el-table-column label="项目编号" align="center" prop="projectCode" width="210" />
      <el-table-column label="项目名称" align="center" prop="projectName" />
      <el-table-column label="项目类别" align="center" prop="projectType">
        <template #default="scope">
          <dict-tag :options="project_type" :value="scope.row.projectType"/>
        </template>
      </el-table-column>
      <el-table-column label="申请单位" align="center" prop="applicationUnitName" />
      <el-table-column label="项目负责人名称" align="center" width="180" prop="projectLeaderName" />
      <el-table-column label="推荐单位" align="center" prop="applicationUnitName" />
      <el-table-column label="项目流程申报状态" align="center" width="180" prop="status">
        <template #default="scope">
          <dict-tag :options="project_app_process_state" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="项目开始时间" align="center" width="180" prop="start" />
      <el-table-column label="更改时间" align="center" prop="updateTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="160" fixed="right">
        <template #default="scope">
          <el-button link type="primary" @click="handleUpdate(scope.row)" v-hasPermi="['departmentWaterResources:project:edit']" v-if="scope.row.status == 0">修改</el-button>
          <el-button link type="primary" @click="handleLook(scope.row)">查看</el-button>
<!--          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['departmentWaterResources:project:remove']">删除</el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />
  </div>
</template>

<script setup name="Project">
import { listProject, delProject, addProject, updateProject } from "@/api/departmentWaterResources/project";

const { proxy } = getCurrentInstance();
const { project_type, project_app_process_state } = proxy.useDict('project_type', 'project_app_process_state');


const router = useRouter();
const projectList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    projectTyoe: null,
    applicationOffice: null,
    projectPersonChargeName: null,
    recommendOffice: null,
    projectApplicationState: null,
    projectStartTime: null,
    projectEndTime: null,
  },
});

const { queryParams } = toRefs(data);

/** 查询项目基本信息列表 */
function getList() {
  loading.value = true;
  listProject(queryParams.value).then(response => {
    projectList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  // reset();
  // open.value = true;
  // title.value = "添加项目基本信息";
  router.push({ path: '/project/infoManagementInfo', query: {id: null, type: 'add'} });
}

/** 修改按钮操作 */
function handleUpdate(row) {
  // reset();
  const _id = row.id || ids.value
  // getProject(_id).then(response => {
  //   form.value = response.data;
  //   open.value = true;
  //   title.value = "修改项目基本信息";
  // });
  router.push({ path: '/project/infoManagementInfo', query: {id: _id, type: 'edit'} });
}

function handleLook(row) {
  router.push({ path: '/project/infoManagementInfo', query: {id: row.id, type: 'look'} });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除项目基本信息编号为"' + _ids + '"的数据项？').then(function() {
    return delProject(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('departmentWaterResources/project/export', {
    ...queryParams.value
  }, `project_${new Date().getTime()}.xlsx`)
}

getList();
</script>
